package com.bdqn.appjob.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.bdqn.appjob.service.backend.BackAppInfoService;
import com.bdqn.appjob.service.backend.BackBackendService;
import com.bdqn.appjob.service.backend.BackDataDictionaryService;
import com.bdqn.appjob.tools.Constants;
import com.bdqn.appjob.tools.PageSupport;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;


import com.bdqn.appjob.entity.AppCategory;
import com.bdqn.appjob.entity.AppInfo;
import com.bdqn.appjob.entity.BackendUser;
import com.bdqn.appjob.entity.DataDictionary;
import com.bdqn.appjob.service.backend.BackAppCategoryService;

@Controller
public class BackendLoginController {
	private Logger logger = Logger.getLogger(BackendLoginController.class);

	// service对象
	@Resource
	private BackBackendService backendService;
	@Resource
	private BackAppInfoService appInfoService;
	@Resource
	private BackAppCategoryService appCategroyService;
	@Resource
	private BackDataDictionaryService dataDictionaryService;

	// 跳转到后台界面
	@RequestMapping(value = "/backendlogin.html")
	public String backendlogin() {
		return "backendlogin";
	}

	// 登录方法
	@RequestMapping(value = "/dologin", method = RequestMethod.POST)
	public String dologin(@RequestParam String userCode, @RequestParam String userPassword, HttpSession session,
			HttpServletRequest request) {
		logger.debug("进来了");
		System.out.println("进了没有");
		BackendUser user = backendService.login(userCode, userPassword);
		if (user != null) {
			// 使用session来保存用户
			session.setAttribute(Constants.USER_SESSION, user);
			// 跳转到主页面
			return "redirect:/backend/main.html";
			// 相当于response.sendRedirect
		} else {
			// 登录失败之后
			throw new RuntimeException("用户名或者密码输入不正确");
		}
	}

	// 登录成功跳转的方法
	@RequestMapping(value = "/backend/main.html")
	public String main(HttpSession session) {
		if (session.getAttribute(Constants.USER_SESSION) != null) {
			return "/backend/main";
		}
		return "backendlogin";
	}

	// 全局异常
	@ExceptionHandler(value = { RuntimeException.class })
	public String handlerException(RuntimeException e, HttpServletRequest request) {
		request.setAttribute("e", e);
		return "backendlogin";
	}

	// 点击注销
	@RequestMapping(value = "/manager/logout")
	public String write(HttpSession session) {
		session.removeAttribute(Constants.USER_SESSION);
		return "backendlogin";
	}

	// 显示数据分页
	@RequestMapping(value = "/manager/backend/app/list")
	public String getlist(Model model, HttpSession session,
			@RequestParam(value = "querySoftwareName", required = false) String softwareName,
			@RequestParam(value = "queryCategoryLevel1", required = false) String queryCategoryLevel1,
			@RequestParam(value = "queryCategoryLevel2", required = false) String queryCategoryLevel2,
			@RequestParam(value = "queryCategoryLevel3", required = false) String queryCategoryLevel3,
			@RequestParam(value = "queryFlatformId", required = false) String flatformId,
			@RequestParam(value = "pageIndex", required = false) Integer pageIndex) {
		logger.debug("pageIndex=" + pageIndex);
		List<AppInfo> appInfolist = null;
		// 列出一级分类列表,二级和三级通过ajax来获取
		List<AppCategory> categoryLevel1List = null;
		List<DataDictionary> flatformList = null;

		// 设置页码容量
		int pageSize = 5;
		// 当前页码
		int currentPageNo = 1;
		
		if (pageIndex != null) {
			currentPageNo = pageIndex;
		}
		Integer queryflatformId = null;
		if (flatformId != null && !flatformId.equals("")) {
			queryflatformId = Integer.parseInt(flatformId);
		}
		Integer _querycategoryLevel1 = null;
		if (queryCategoryLevel1 != null && !queryCategoryLevel1.equals("")) {
			_querycategoryLevel1 = Integer.parseInt(queryCategoryLevel1);
		}
		Integer _querycategoryLevel2 = null;
		if (queryCategoryLevel2 != null && !queryCategoryLevel2.equals("")) {
			_querycategoryLevel2 = Integer.parseInt(queryCategoryLevel2);
		}
		Integer _querycategoryLevel3 = null;
		if (queryCategoryLevel3 != null && !queryCategoryLevel3.equals("")) {
			_querycategoryLevel3 = Integer.parseInt(queryCategoryLevel3);
		}
		
		// 总数量
		int totalCount = appInfoService.getCount(softwareName, queryflatformId, _querycategoryLevel1, _querycategoryLevel2,
				_querycategoryLevel3);
		
		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);
		
		int totalPageCount = pages.getTotalPageCount();
		// 控制首页和尾页
		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}
		
		appInfolist = appInfoService.getAllList(softwareName, queryflatformId, _querycategoryLevel1, _querycategoryLevel2,
				_querycategoryLevel3, (currentPageNo-1)*pageSize, pageSize);
		
		categoryLevel1List = appCategroyService.getListId(null);
		
		flatformList = dataDictionaryService.getDict();			//所属平台
		
		model.addAttribute("softwareName",softwareName);
		model.addAttribute("flatFormList", flatformList);
		model.addAttribute("categoryLevel1List",categoryLevel1List);
		model.addAttribute("pages",pages);
		model.addAttribute("queryCategoryLevel1",_querycategoryLevel1);
		if(_querycategoryLevel2 != null && !_querycategoryLevel2.equals("")){
			model.addAttribute("queryCategoryLevel2",_querycategoryLevel2);
		}
		if(_querycategoryLevel3 != null && !_querycategoryLevel3.equals("")){
			model.addAttribute("queryCategoryLevel3",_querycategoryLevel3);
		}
		
		model.addAttribute("appInfoList", appInfolist);
		return "backend/applist";
	}	

}
